package com.zby.demo.controller.back;

import com.zby.demo.dao.CourseDao;
import com.zby.demo.dao.TeacherDao;
import com.zby.demo.entity.Course;
import com.zby.demo.entity.Teacher;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/23 11:39
 */

@Controller
public class TeacherController {
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    CourseDao courseDao;

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:32
     * @Description: 转发到教师列表页面
     */
    @RequestMapping("/back/teacher/list")
    public String toStudentList(Model model) {
        List<Teacher> teachers = teacherDao.queryAllTeacher();
        model.addAttribute("teachers", teachers);
        List<Course> teachcourses = new ArrayList<>();
        for (Teacher teacher : teachers) {
            teachcourses.add(teacherDao.queryTeachCourseByID(teacher.getTeacherId()));
        }
        model.addAttribute("teachcourses", teachcourses);
        List<Course> courses = courseDao.queryAllCourse();
        model.addAttribute("courses", courses);
        return "back/teacher/list";
    }

    /*
     * @Param: [teacher, teachcourseId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:35
     * @Description: 添加教师
     */
    @PostMapping("/back/teacher/add")
    public String addStudent(Teacher teacher, @Param("teachcourseId") String teachcourseId) {
        teacher.setPassword("zut" + teacher.getTeacherId().substring(teacher.getTeacherId().length() - 6));
        if (teacher.getSex() == null) {
            teacher.setSex("其他");
        }
        teacherDao.addTeacher(teacher);
        teacherDao.addTeachCourse(teacher.getTeacherId(), teachcourseId);
        return "redirect:/back/teacher/list";
    }

    /*
     * @Param: [teacherId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:38
     * @Description: 删除指定教师。注意点：需要先删除对应的授课表的内容
     */
    @GetMapping("/back/teacher/delete/{teacherId}")
    public String deleteStudent(@PathVariable("teacherId") String teacherId) {
        teacherDao.deleteTeachCourseByID(teacherId);
        teacherDao.deleteTeacher(teacherId);
        return "redirect:/back/teacher/list";
    }

    /*
     * @Param: [deleteall]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:39
     * @Description: 批量删除教师。注意点同上
     */
    @PostMapping("/back/teacher/delete")
    public String deleteTeachers(@Param("deleteall") String deleteall) {
        String[] deleteTeacherIds = deleteall.split(",");
        for (String teacherId : deleteTeacherIds) {
            teacherDao.deleteTeachCourseByID(teacherId);
            teacherDao.deleteTeacher(teacherId);
        }
        return "redirect:/back/teacher/list";
    }

    /*
     * @Param: [teacher, teachcourseId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:40
     * @Description: 修改教师信息
     */
    @PostMapping("/back/teacher/info")
    public String updateStudent(Teacher teacher, @Param("teachcourseId") String teachcourseId) {
        teacherDao.updateTeacherInfo(teacher);
        teacherDao.updateTeacherTeachCourse(teacher.getTeacherId(), teachcourseId);
        return "redirect:/back/teacher/list";
    }

    /*
     * @Param: [teacherId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:40
     * @Description: 修改教师密码。
     */
    @GetMapping("/back/teacher/{teacherId}")
    public String resetStudentPassword(@PathVariable("teacherId") String teacherId) {
        String password = "zut" + teacherId.substring(teacherId.length() - 6);
        teacherDao.updateTeacherPassword(teacherId, password);
        return "redirect:/back/teacher/list";
    }
}
